package com.myproject0616chat;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

public class MainInterface extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JScrollPane dialogbox;
	private JTextField srtf;
	private Object message;
	private JTextArea jta;
	private Receriver receriver;
	// private JScrollBar bar;
	Font font = new Font("微软雅黑", Font.BOLD, 20);
	Font fon = new Font("楷体", Font.PLAIN, 18);

	public MainInterface() {
		setTitle("聊天栏");
		setSize(500, 500);
		setVisible(true);
		setLocation(400, 200);
		setLayout(new BorderLayout());
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		jta = new JTextArea();
		jta.setBackground(new Color(0xA9A9A9));
		jta.setEditable(false);
		jta.setDisabledTextColor(Color.blue);
		jta.setFont(fon);
		jta.setLineWrap(true); // 设置文本域超过显示宽度自动换行
		dialogbox = new JScrollPane(jta);
		dialogbox.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		// bar=dialogbox.getVerticalScrollBar();
		add(dialogbox, BorderLayout.CENTER);

		JPanel inputbox = new JPanel();
		inputbox.setLayout(new BorderLayout());
		add(inputbox, BorderLayout.SOUTH);

		srtf = new JTextField();
		srtf.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		srtf.setFont(fon);
		srtf.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyPressed(e);
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					if (srtf.getText().trim().equals(""))
						return;
					SocketUtil.sendMessage(srtf.getText());
					srtf.setText("");

				}
			}
		});
		inputbox.add(srtf, BorderLayout.CENTER);
		JButton fsbt = new JButton("发送");
		fsbt.setFont(font);
		fsbt.addActionListener(this);

		fsbt.setBackground(new Color(0xBBFFFF));
		fsbt.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		inputbox.add(fsbt, BorderLayout.EAST);

		receriver = new Receriver(jta);
		SocketUtil.setReceriver(receriver);
		SocketUtil.linking();
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new MainInterface();
			}
		});

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		String cmd = e.getActionCommand();
		message = srtf.getText();
		String str = (String) message; // message强制转换成string类型
		if (str.trim().equals(""))
			return; // trim去掉空格

		if ("发送".indexOf(cmd) >= 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日hh:mm:ss:SSS");
			String s = sdf.format(new Date());
			// jta.append("我"+":/"+s+":\n"+message+"\n");
			// //append功能有点像setText，但是不会覆盖掉原来的已有的信息
			SocketUtil.sendMessage(message);
			//

			srtf.setText("");

		}

	}
}
